# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=librsvg
pkgbase=mingw-w64-${_realname}-2.40
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}-2.40"
pkgver=2.40.21
pkgrel=1
pkgdesc="A SVG viewing library (mingw-w64)"
arch=('any')
# only for arches where rust doesn't work (otherwise use librsvg)
mingw_arch=('clang32' 'clangarm64')
url="https://live.gnome.org/LibRsvg"
license=("LGPL")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
            "${MINGW_PACKAGE_PREFIX}-gdk-pixbuf2"
            "${MINGW_PACKAGE_PREFIX}-gobject-introspection"
            "${MINGW_PACKAGE_PREFIX}-gtk-doc"
            "${MINGW_PACKAGE_PREFIX}-autotools"
            "${MINGW_PACKAGE_PREFIX}-vala"
            "setconf")
depends=("${MINGW_PACKAGE_PREFIX}-gdk-pixbuf2"
         "${MINGW_PACKAGE_PREFIX}-pango"
         "${MINGW_PACKAGE_PREFIX}-libcroco")
optdepends=("${MINGW_PACKAGE_PREFIX}-gtk3: for rsvg-view-3")
provides=("${MINGW_PACKAGE_PREFIX}-${_realname}")
conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}")
options=('staticlibs' 'strip')
source=("https://download.gnome.org/sources/librsvg/${pkgver%.*}/${_realname}-${pkgver}.tar.xz"
        "0005-hack-unixy-paths.patch"
        "0006-fix-vapigen-deps.patch")
sha256sums=('f7628905f1cada84e87e2b14883ed57d8094dca3281d5bcb24ece4279e9a92ba'
            'b23b094c0cb65fcbbbb952350448de6f3430b30f273e05acdbf7a56d634212dc'
            '2c958c551a6bee4b084fdd82c6b08754927a2221f03930e2193fe45d62883b04')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"

  patch -p1 -i "${srcdir}/0005-hack-unixy-paths.patch"

  # https://gitlab.gnome.org/GNOME/vala/-/issues/1199
  patch -p1 -i "${srcdir}/0006-fix-vapigen-deps.patch"

  autoreconf -fi
}

build() {
  [[ -d "${srcdir}/build-${MINGW_CHOST}" ]] && rm -rf "${srcdir}/build-${MINGW_CHOST}"
  cp -r ${srcdir}/${_realname}-${pkgver} "${srcdir}/build-${MINGW_CHOST}"
  cd "${srcdir}/build-${MINGW_CHOST}"

  mkdir -p doc/html
  cp -rf ../${_realname}-${pkgver}/doc/html/* doc/html

  CXXFLAGS+=" -D_POSIX_SOURCE"
  CFLAGS+=" -D_POSIX_SOURCE"

  # an argument passed to glib-mkenums starts with a c++ comment
  export MSYS2_ARG_CONV_EXCL="/*"

  ../${_realname}-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --enable-introspection \
    --enable-vala \
    --enable-silent-rules \
    --disable-Bsymbolic
  setconf Makefile AM_CFLAGS '-fno-common'
  make
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  make DESTDIR="${pkgdir}" install

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/COPYING" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING"
}
